from django.shortcuts import render
from django.core.paginator import Paginator,Page,EmptyPage,PageNotAnInteger
from weiboRecommendation.models import WeiboMessage
# Create your views here.
from sentence_transformers.util import cos_sim
from sentence_transformers import SentenceTransformer as SBert




def firstFunc(request):
    # msg_list = []
    # for i in range(11):
    #     msg = {
    #         'time':str(i)+"的发布时间",
    #         'author':'Author'+str(i),
    #         'message':str(i)+"的内容",
    #         "reviewAmount":i
    #     }
    #     msg_list.append(msg)
    # context = {
    #     'msg_list':msg_list
    # }
    # return render(request,'firstPage.html',context)




    msg_list = WeiboMessage.objects.all()  # 取出表中数据
    paginator = Paginator(msg_list, 20)  # 一页20个
    page = request.GET.get('page')  # 获取当前page 注意GET 不是Get
    print("msg_list中的一个实例:",msg_list[0])
    data_list = []
    if page:
        data_list = paginator.page(page).object_list

    else:
        data_list = paginator.page(1).object_list
    try:  # 判断页码不存在，返回当前页码对象
        page_object = paginator.page(page)
    except PageNotAnInteger:
        page_object = paginator.page(1)
    except EmptyPage:
        page_object = paginator.page(paginator.num_pages)

    # cnt = 0
    # for data in data_list:
    #     print(data.msg_url,data.author_main_page,data.msg_content,data.msg_time,data.msg_author)
    #     print()
    #     cnt+=1
    #     if cnt>4:
    #         break
    # print(page_object)
    print(page_object)
    return render(request, "firstPage.html", {
        'page_object': page_object,
        'data_list': data_list
    })
#
# def secondFunc(request):
#     msg_list = WeiboMessage.objects.all()  # 取出DoubanMoview表所有数据
#     print(len(msg_list))
#     paginator = Paginator(msg_list, 20)  # 20是每页显示的数量，把数据库取出的数据生成paginator对象，并指定每页显示的数量
#     page = request.GET.get('page')  # 从查询字符串获取page的当前页数
#     data_list = []
#     if page:  # 判断：获取当前页码的数据集，这样在模版就可以针对当前的数据集进行展示
#         data_list = paginator.page(page).object_list
#     else:
#         data_list = paginator.page(1).object_list
#     try:  # 实现分页对象，分别判断当页码存在/不存在的情况，返回当前页码对象
#         page_object = paginator.page(page)
#     except PageNotAnInteger:
#         page_object = paginator.page(1)
#     except EmptyPage:
#         page_object = paginator.page(paginator.num_pages)
#     if data_list:  # 实现相似微博的推荐
#         msg_content_list = msg_list.objects.values_list('msg_content')  # 取出content内容
#         # 使用模型
#         modelBert = SBert('paraphrase-multilingual-MiniLM-L12-v2')
#         for data in data_list:
#             # 计算嵌入并保存
#             data.embeddings = modelBert.encode(data.msg_content)
#         for data in data_list:
#             cosine_scoretop = 0
#             msg_top = ""  # 记录top 1的结果
#             for data_rec in data_list:
#                 if data.msg_content != data_rec.msg_content:
#                     cosine_scores = cos_sim(data.embeddings, data_rec.embeddings)
#                     if cosine_scores > cosine_scoretop:
#                         cosine_scoretop = cosine_scores
#                         msg_top = data_rec.msg_content
#             data.msg_top = msg_top
#     return render(request, "firstPage.html", {
#         'page_object': page_object,
#         'data_list': data_list
#     })




